package datebase;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.JFrame;

public class Search extends JInternalFrame {
	PreparedStatement pstmtTotalScroll, pstmtTotal;

	Connection con;
	private JPanel panel;
	private JButton btnNewButton;
	private JTextField text;
	private JScrollPane scrollPane;
	private JTable table;
	DefaultTableModel model = new DefaultTableModel();
	String[] columnName;
	String[] columnType;
	Object[][] cell_value;
	ResultSet rs;
	ResultSet rsScroll;
	ResultSetMetaData rsmd;
	Update up;
	DatabaseTest d;
	String sql;

	public Search(Connection c, DatabaseTest d) {
		super("search", true, true, true, false);
		this.con = c;
		this.d = d;
		setBounds(100, 100, 707, 485);
		getContentPane().add(getPanel(), BorderLayout.NORTH);
		getContentPane().add(getScrollPane(), BorderLayout.CENTER);

	}

	void search() {
		try {

			model.setColumnCount(0);
			model.setRowCount(0);
			if (text.getText().equals("")) {
				sql = "select * from ptbl order by serial";
			} else {
				sql = text.getText();
			}
			Statement stmt = con.createStatement();
			pstmtTotalScroll = con
					.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE,
							ResultSet.CONCUR_UPDATABLE);
			pstmtTotal = con.prepareStatement(sql);

			try {
				if (sql.toLowerCase().indexOf("select") >= 0) {
					rsScroll = pstmtTotalScroll.executeQuery();
				}
				rs = pstmtTotal.executeQuery();

			} catch (SQLException e) {
				e.printStackTrace();
			}//공부하시오 -송태진

			rsmd = rs.getMetaData();
			int cols = rsmd.getColumnCount();
			rsScroll.last();
			String[] columnNam = new String[cols];
			Vector<String> columnName = new Vector<String>();
			Vector<String> column = new Vector<String>();

			for (int i = 0; i < cols; i++) {
				model.addColumn(rsmd.getColumnName(i + 1));
				columnNam[i] = rsmd.getColumnName(i + 1);
			}
			while (rs.next()) {
				Vector<String> row = new Vector<String>();
				for (int i = 0; i < cols; i++) {
					row.add(rs.getString(i + 1));
				}
				model.addRow(row);
			}
			
			rs.close();
			stmt.close();
		} catch (Exception e) {
			System.out.println("예외사항:" + e.getMessage());
		}
	}

	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getBtnNewButton(), BorderLayout.EAST);
			panel.add(getText(), BorderLayout.CENTER);
		}
		return panel;
	}

	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("쿼리실행");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					search();
				}
			});
		}
		return btnNewButton;
	}

	public void MouseC() {
		int index = table.getSelectedRow();
		Vector<String> vec = new Vector<String>();
		vec.add((String) table.getValueAt(index, 0));
		vec.add((String) table.getValueAt(index, 1));
		vec.add((String) table.getValueAt(index, 2));
		
		if (d.update == null) {
			d.update = new Update(con, d);
			d.contentPane.add(d.update);
		    d.update.setVisible(true);
			d.update.toFront();
		}
	}

	private JTextField getText() {
		if (text == null) {
			text = new JTextField();
			text.setColumns(10);
		}
		return text;
	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}

	private JTable getTable() {
		if (table == null) {
			table = new JTable(model);
			table.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					MouseC();
				}
			});
		}
		return table;
	}

	public void setD() {
		search();
	}

}